package org.kunlong.netix.util.log.stringmanager;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

/**
 * a StringManager manage all the log string
 * @version 1.0
 * @author Kent_G
 * */
public class StringManager {
	private static Logger logger=Logger.getLogger(StringManager.class);
	private static Map<String,LogStringMap> map=new HashMap<String,LogStringMap>();
	public static String getLogString(String packageName,String key){
		if(!map.containsKey(packageName)){
			//if the package has not been loaded yet.
			synchronized (map) {
				if(!map.containsKey(packageName)){
					//in case of one thread just put in the LogStringMap,
					//so double check here
					LogStringMap logStringMap=new LogStringMap();
					logStringMap.setPackageName(packageName);
					try{
					logStringMap.load();
					}catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
						logger.error("Error Loading package log string["+packageName+"]",e);
					}
					map.put(packageName, logStringMap);
				}
			}
		}
		return map.get(packageName).getString(key);
	}
	public static void main(String args[]){
		String message=StringManager.getLogString(StringManager.class.getPackage().getName(), "test");
		System.out.println(message);
	}
}
